package cn.jtool.dig.server.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.jtool.dig.server.domain.reqeust.LoginParams;
import cn.jtool.dig.server.service.SessionService;
import cn.jtool.dig.server.service.impl.SessionServiceImpl;
import cn.jtool.knife.core.result.ResultEntity;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/session")
public class SessionController {

    @Resource
    private SessionService sessionService;

    @PostMapping("/login")
    public ResponseEntity<ResultEntity<?>> login(@Validated @RequestBody LoginParams params){
         return ResponseEntity.ok(
                 ResultEntity.success(
                         this.sessionService.login(params)
                 )
         );
    }

    @GetMapping("/info")
    public ResponseEntity<ResultEntity<?>> info(){
        return ResponseEntity.ok(
                ResultEntity.success(
                        this.sessionService.info(StpUtil.getLoginIdAsInt())
                )
        );
    }

    @GetMapping("/logout")
    public ResponseEntity<ResultEntity<?>> logout(){
        if(StpUtil.isLogin()) {
            this.sessionService.logout(StpUtil.getLoginIdAsInt());
        }
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }


}
